package com.quincy.controller;

import com.quincy.enums.VideoStatusEnum;
import com.quincy.pojo.Bgm;
import com.quincy.pojo.Comments;
import com.quincy.pojo.Videos;
import com.quincy.service.BgmService;
import com.quincy.service.VideoService;
import com.quincy.utils.FetchVideoCover;
import com.quincy.utils.JsonResult;
import com.quincy.utils.MergeVideoMp3;
import com.quincy.utils.PagedResult;
import io.swagger.annotations.*;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Date;
import java.util.UUID;

/**
 * Created by Quincy on 2018/5/26.
 */

@RestController
@Api(value="视频相关业务的接口", tags= {"视频相关业务的controller"})
@RequestMapping("/video")
public class VideoController extends BasicController{

    @Autowired
    private BgmService bgmService;

    @Autowired
    private VideoService videoService;

    @ApiOperation(value="上传视频", notes="上传视频的接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name="userId", value="用户id", required=true,
                    dataType="String", paramType="form"),
            @ApiImplicitParam(name="bgmId", value="背景音乐id", required=false,
                    dataType="String", paramType="form"),
            @ApiImplicitParam(name="videoSeconds", value="背景音乐播放长度", required=true,
                    dataType="String", paramType="form"),
            @ApiImplicitParam(name="videoWidth", value="视频宽度", required=true,
                    dataType="String", paramType="form"),
            @ApiImplicitParam(name="videoHeight", value="视频高度", required=true,
                    dataType="String", paramType="form"),
            @ApiImplicitParam(name="desc", value="视频描述", required=false,
                    dataType="String", paramType="form")
    })
    @PostMapping(value="/upload", headers="content-type=multipart/form-data")
    public JsonResult upload(String userId,
                                  String bgmId, double videoSeconds,
                                  int videoWidth, int videoHeight,
                                  String desc,
                                  @ApiParam(value="短视频", required=true)
                                     MultipartFile file) throws Exception {

        if (StringUtils.isBlank(userId)) {
            return JsonResult.errorMsg("用户id不能为空...");
        }

        // 文件保存的命名空间
//		String fileSpace = "C:/imooc_videos_dev";
        // 保存到数据库中的相对路径
        String uploadPathDB = "/" + userId + "/video";
        String coverPathDB = "/" + userId + "/video";

        FileOutputStream fileOutputStream = null;
        InputStream inputStream = null;
        // 文件上传的最终保存路径
        String finalVideoPath = "";
        try {
            if (file != null) {

                String fileName = file.getOriginalFilename();
                // abc.mp4
                String fileNamePrefix = fileName.split("\\.")[0];

                if (StringUtils.isNotBlank(fileName)) {

                    finalVideoPath = FILE_SPACE + uploadPathDB + "/" + fileName;
                    // 设置数据库保存的路径
                    uploadPathDB += ("/" + fileName);
                    coverPathDB = coverPathDB + "/" + fileNamePrefix + ".jpg";

                    File outFile = new File(finalVideoPath);
                    if (outFile.getParentFile() != null || !outFile.getParentFile().isDirectory()) {
                        // 创建父文件夹
                        outFile.getParentFile().mkdirs();
                    }

                    fileOutputStream = new FileOutputStream(outFile);
                    inputStream = file.getInputStream();
                    IOUtils.copy(inputStream, fileOutputStream);
                }

            } else {
                return JsonResult.errorMsg("上传出错...");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.errorMsg("上传出错...");
        } finally {
            if (fileOutputStream != null) {
                fileOutputStream.flush();
                fileOutputStream.close();
            }
        }

        // 判断bgmId是否为空，如果不为空，
        // 那就查询bgm的信息，并且合并视频，生产新的视频
        if (StringUtils.isNotBlank(bgmId)) {
            Bgm bgm = bgmService.queryBgmById(bgmId);
            String mp3InputPath = FILE_SPACE + bgm.getPath();

            MergeVideoMp3 tool = new MergeVideoMp3(FFMPEG_EXE);
            String videoInputPath = finalVideoPath;

            String videoOutputName = UUID.randomUUID().toString() + ".mp4";
            uploadPathDB = "/" + userId + "/video" + "/" + videoOutputName;
            finalVideoPath = FILE_SPACE + uploadPathDB;
            tool.convertor(videoInputPath, mp3InputPath, videoSeconds, finalVideoPath);
        }
        System.out.println("uploadPathDB=" + uploadPathDB);
        System.out.println("finalVideoPath=" + finalVideoPath);

        // 对视频进行截图
        FetchVideoCover videoInfo = new FetchVideoCover(FFMPEG_EXE);
        videoInfo.getCover(finalVideoPath, FILE_SPACE + coverPathDB);

        // 保存视频信息到数据库
        Videos video = new Videos();
        video.setAudioId(bgmId);
        video.setUserId(userId);
        video.setVideoSeconds((float)videoSeconds);
        video.setVideoHeight(videoHeight);
        video.setVideoWidth(videoWidth);
        video.setVideoDesc(desc);
        video.setVideoPath(uploadPathDB);
        video.setCoverPath(coverPathDB);
        video.setStatus(VideoStatusEnum.SUCCESS.value);
        video.setCreateTime(new Date());

        String videoId = videoService.saveVideo(video);

        return JsonResult.ok(videoId);
    }

    @ApiOperation(value="上传封面", notes="上传封面的接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name="userId", value="用户id", required=true,
                    dataType="String", paramType="form"),
            @ApiImplicitParam(name="videoId", value="视频主键id", required=true,
                    dataType="String", paramType="form")
    })
    @PostMapping(value="/uploadCover", headers="content-type=multipart/form-data")
    public JsonResult uploadCover(String userId,
                                       String videoId,
                                       @ApiParam(value="视频封面", required=true)
                                               MultipartFile file) throws Exception {

        if (StringUtils.isBlank(videoId) || StringUtils.isBlank(userId)) {
            return JsonResult.errorMsg("视频主键id和用户id不能为空...");
        }

        // 文件保存的命名空间
//		String fileSpace = "C:/imooc_videos_dev";
        // 保存到数据库中的相对路径
        String uploadPathDB = "/" + userId + "/video";

        FileOutputStream fileOutputStream = null;
        InputStream inputStream = null;
        // 文件上传的最终保存路径
        String finalCoverPath = "";
        try {
            if (file != null) {

                String fileName = file.getOriginalFilename();
                if (StringUtils.isNotBlank(fileName)) {

                    finalCoverPath = FILE_SPACE + uploadPathDB + "/" + fileName;
                    // 设置数据库保存的路径
                    uploadPathDB += ("/" + fileName);

                    File outFile = new File(finalCoverPath);
                    if (outFile.getParentFile() != null || !outFile.getParentFile().isDirectory()) {
                        // 创建父文件夹
                        outFile.getParentFile().mkdirs();
                    }

                    fileOutputStream = new FileOutputStream(outFile);
                    inputStream = file.getInputStream();
                    IOUtils.copy(inputStream, fileOutputStream);
                }

            } else {
                return JsonResult.errorMsg("上传出错...");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.errorMsg("上传出错...");
        } finally {
            if (fileOutputStream != null) {
                fileOutputStream.flush();
                fileOutputStream.close();
            }
        }

        videoService.updateVideo(videoId, uploadPathDB);

        return JsonResult.ok();
    }

    /***
     * @Description : 分页和搜索查询视频列表
     * isSavedRecord: 1 - 需要保存
     *                0 - 不需要保存，或者为空的时候
     */
    @PostMapping(value="/showAll")
    public JsonResult showAll(@RequestBody Videos video, Integer isSaveRecord, Integer page, Integer pageSize) throws Exception {

        if (page == null) {
            page = 1;
        }

        if (pageSize == null){
            pageSize = PAGE_SIZE;
        }

        PagedResult result = videoService.getAllVideos(video, isSaveRecord, page, PAGE_SIZE);
        return JsonResult.ok(result);
    }

    /**
     * @Description :热搜词排序查询
     *
     * */
    @PostMapping(value = "/hot")
    public JsonResult hot() throws Exception{
        return JsonResult.ok(videoService.getHotwords());
    }

    /**
     * @Description :我关注的人发的视频列表
     * */
    @PostMapping("/showMyFollow")
    public JsonResult showMyFollow(String userId, Integer page) throws Exception{
        if (StringUtils.isBlank(userId)){
            return JsonResult.ok();
        }
        if (page == null){
            page = 1;
        }
        int pageSize = 6;
        PagedResult videoList = videoService.queryMyFollowVideos(userId, page, pageSize);
        return JsonResult.ok(videoList);
    }

    /**
     * @Description :我收藏（点赞）过的视频列表
     * */
    @PostMapping("/showMyLike")
    public JsonResult showMyLike(String userId, Integer page, Integer pageSize) throws Exception{
        if (StringUtils.isBlank(userId)){
            return JsonResult.ok();
        }
        if (page == null){
            page = 1;
        }
        if (pageSize == null){
            pageSize = 6;
        }
        PagedResult videoList = videoService.queryMyLikeVideos(userId, page, pageSize);
        return JsonResult.ok(videoList);
    }


    @PostMapping(value = "/userLike")
    public JsonResult userLike(String userId, String videoId, String videoCreaterId) throws Exception{
        videoService.userLikeVideo(userId,videoId,videoCreaterId);
        return JsonResult.ok();
    }

    @PostMapping(value = "/userUnLike")
    public JsonResult userUnLike(String userId, String videoId, String videoCreaterId) throws Exception{
        videoService.userUnLikeVideo(userId,videoId,videoCreaterId);
        return JsonResult.ok();
    }

    @PostMapping("/saveComment")
    public JsonResult saveComment(@RequestBody Comments comment, String fatherCommentId, String toUserId) throws Exception{

        if (StringUtils.isNotBlank(fatherCommentId)){
            comment.setFatherCommentId(fatherCommentId);
        }
        if (StringUtils.isNotBlank(toUserId)){
            comment.setToUserId(toUserId);
        }
        videoService.saveComment(comment);
        return JsonResult.ok();
    }

    @PostMapping("/getVideoComments")
    public JsonResult getVideoComments(String videoId, Integer page, Integer pageSize) throws Exception{
        if (StringUtils.isBlank(videoId)){
            return JsonResult.ok();
        }
        //分页查询视频列表，时间顺序倒序排序
        if (page == null){
            page = 1;
        }
        if (pageSize == null){
            pageSize = 10;
        }

        PagedResult list = videoService.getAllComments(videoId, page, pageSize);

        return JsonResult.ok(list);
    }
}
